package com.gdut.simple.presenter;

import com.gdut.simple.R;
import com.gdut.simple.basic.BasicApplication;
import com.gdut.simple.common.NetworkUtils;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.contract.PublishContract;
import com.gdut.simple.model.IPublishModel;
import com.gdut.simple.model.impl.PublishModelImpl;


public class PublishPresenter implements PublishContract.Presenter {

    private PublishContract.View mPublishView;
    private IPublishModel mPublishModel;

    public PublishPresenter(PublishContract.View view) {
        this.mPublishView = view;
        mPublishModel = new PublishModelImpl();
        mPublishView.setPresenter(this);
    }

    @Override
    public void publish() {
        if (!NetworkUtils.isConnected(BasicApplication.getApplication())) {
            mPublishView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_error_net));
            return;
        }
        if (mPublishView.getUser() == null) {
            mPublishView.showMessage("user is null");
            return;
        }
        if (mPublishView.getContent() == null || mPublishView.getContent().isEmpty()) {
            mPublishView.showMessage("发表的内容不能为空");
            return;
        }
        mPublishView.showProgress("发表中...");
        mPublishModel.publish(mPublishView.getUser(),
                mPublishView.getImagePaths(),
                mPublishView.getContent(),
                new RequestCallback<String>() {
                    @Override
                    public void onSuccess(String s) {
                        mPublishView.hideProgress();
                        mPublishView.showMessage("发表成功");
                        mPublishView.next();
                    }

                    @Override
                    public void onFailure(String message) {
                        mPublishView.hideProgress();
                        mPublishView.showMessage(message);
                    }
                });
    }

}
